<?php //functions to add something to the order 
if(isset($_POST['add_item'])){
    $number = $_POST['number'];
    $cat = $_POST['cat'];
    $name = $_POST['name'];
    $type = $_POST['type'];
    $price = $_POST['price'];
    $VAT = $_POST['VAT'];
    $quantity = $_POST['amount'];
    $id = $_POST['id'];
    $discount = $_POST['discount'];
    addItem($number, $cat, $name, $type, $price, $VAT, $quantity, $id, $discount);
    unset($_POST['add_item']);
}
function addItem($number, $cat, $name, $type, $price, $VAT, $quantity,$id, $discount){
    //check if in list
    if(isset($_SESSION['orderlist'])){
        $inlist = false; //standard value
        foreach($_SESSION['orderlist'] as $value){  //go through list
            if($id == $value['id']){
                if($cat == $value['cat']){
                    $inlist = true;
                    echo '<span style="color:red;">The item has already been added to the list</span>';
                    break;
                }
            }
        }
        if($inlist== false){
            setVariables($number, $cat, $name, $type, $price, $VAT, $quantity,$id, $discount);
        }
    } else {
        setVariables($number, $cat, $name, $type, $price, $VAT, $quantity,$id, $discount);
    }
}
function setVariables($number, $cat, $name, $type, $price, $VAT, $quantity,$id, $discount){
    $i = 0;
    while(isset($_SESSION['orderlist'][$i])){
        $i++;
    }
    $array = array('id'=>$id, 'cat'=>$cat, 'number'=>$number, 'name'=>$name, 'type'=>$type, 'price'=>$price, 'VAT'=>$VAT, 'amount'=>$quantity, 'discount'=> $discount);
    $_SESSION['orderlist'][$i] = $array;
}
if(isset($_POST['empty_order'])){
    if(isset($_SESSION['orderlist'])){
        unset($_SESSION['orderlist']);
    }
}
if(isset($_POST['update'])){
    $i = 0;
    while(isset($_POST[$i])){
        $_SESSION['orderlist'][$i]['amount'] = $_POST['amount'.$i.''];
        $_SESSION['orderlist'][$i]['discount'] = $_POST['discount'.$i.''];
        $i++;
    }
    //unset($_POST['update']);
}
if( !$form->getObject()->isNew() ){
    $_SESSION['page'] = true;
    $order = $form->getObject();
    $totalPrice = $order->getTotalPrice();
    $_SESSION['price'] = $totalPrice;
}
if(isset($_POST['page'])){
    $_SESSION['page']= true;
    $_SESSION['price'] = $_POST['totalprice'];
}
?>
<!-- the actual form, for normal order -->
<?php //check if page is the cart or the fill in of what contact
    if(!isset($_SESSION['page'])){
        //show all the possible products plus the shopping cart 
        ?>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
            <fieldset>  
                <legend class="bold">Materials &amp; Products for sale</legend>
                <table class="table_top">
                    <tr class="bold head_small">
                        <td width="70px">Item No.</td>
                        <td width="60px">Category</td>
                        <td width="200px">Name</td>
                        <td>Type</td>
                        <td width="120px">Price per Unit (excl. VAT)</td>
                        <td width="40px">VAT(%)</td>
                        <td>Quantity</td>
                        <td>Discount(%)</td>
                        <td width="35px"></td>
                    </tr>
                </table>
                <div id="scroll_div" class="table_div">
                    <table>
                        <!-- ugly solution to get the table right -->
                        <tr class="bold head_small" height="15px">
                            <td width="70px"></td>
                            <td width="60px"></td>
                            <td width="200"></td>
                            <td></td>
                            <td width="100px"></td>
                            <td width="40px"></td>
                            <td></td>
                            <td></td>
                            <td width="20px"></td>
                        </tr>
                    <?php foreach ($parts as $i=>$material): ?>
                        <tr class="table_hov <?php echo fmod($i, 2) ? 'even' : 'odd' ?> ">
                            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                                <input type="hidden" name="number" value="<?php echo $material->getPartNumber() ?>"/>
                                <input type="hidden" name="cat" value="<?php echo 'Material' ?>"/>
                                <input type="hidden" name="name" value="<?php echo $material->getName() ?>"/>
                                <input type="hidden" name="price" value="<?php echo $material->getPrice() ?>"/>
                                <input type="hidden" name="VAT" value="<?php echo $material->getVAT() ?>"/>
                                <input type="hidden" name="type" value="<?php echo $material->getType() ?>"/> 
                                <input type="hidden" name="id" value="<?php echo $material->getId() ?>"/>
                                <td><?php echo $material->getPartNumber() ?></td>
                                <td><?php echo 'Material' ?></td>
                                <td><?php echo $material->getName() ?></td>
                                <td><?php echo $material->getType() ?></td>
                                <td><?php echo $material->getPrice() ?></td>
                                <td><?php echo $material->getVAT() ?></td>
                                <td><input type="text" value="0" name="amount" size="3"/></td>
                                <td><input type="text" value="0" name="discount" size="3"/></td>
                                <td><input type="submit" class="add_bt" value="" name="add_item" title="Add to order"/></td>
                            </form>
                        </tr>
                    <?php endforeach; ?>
                    <?php foreach ($products as $i=>$product): ?> 
                        <tr class="table_hov <?php echo fmod($i, 2) ? 'even' : 'odd' ?> ">
                            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                                <input type="hidden" name="number" value="<?php echo $product->getProductNumber() ?>"/>
                                <input type="hidden" name="cat" value="<?php echo 'Product' ?>"/>
                                <input type="hidden" name="name" value="<?php echo $product->getName() ?>"/>
                                <input type="hidden" name="price" value="<?php echo $product->getPrice() ?>"/>
                                <input type="hidden" name="VAT" value="<?php echo $product->getVAT() ?>"/>
                                <input type="hidden" name="type" value="<?php echo $product->getType() ?>"/> 
                                <input type="hidden" name="id" value="<?php echo $product->getId() ?>"/>
                                <td><?php echo $product->getProductNumber() ?></td>
                                <td><?php echo 'Product' ?></td>
                                <td><?php echo $product->getName() ?></td>
                                <td><?php echo $product->getType() ?></td>
                                <td><?php echo $product->getPrice() ?></td>
                                <td><?php echo $product->getVAT() ?></td>
                                <td><input type="text" value="0" name="amount" size="3"/></td>
                                <td><input type="text" value="0" name="discount" size="3"/></td>
                                <td><input type="submit" class="add_bt" value="" name="add_item" title="Add to order"/></td>
                            </form>
                        </tr>
                    <?php endforeach; ?>    
                    </table>
                </div>
            </fieldset>
        </form>
        <?php if(isset($_SESSION['orderlist'])){ ?>
            <fieldset>
                <legend class="bold">Order list</legend>
                    <table>
                        <tr class="bold head_small">
                            <td width="50px">Item No.</td>
                            <td width="60px">Category</td>
                            <td>Name</td>
                            <td>Type</td>
                            <td width="80px">Price per Unit (excl. VAT)</td>
                            <td>Quantity</td>
                            <td width="40px">VAT(%)</td>
                            <td width="55px">Discount(%)</td>
                            <td width="80px" style="text-align: right;">Total price</td>
                        </tr>
                        <?php foreach($_SESSION['orderlist'] as $value){ ?>
                        <tr>
                        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                            <?php
                                if(!isset($totalprice)){
                                    $totalprice = 0;
                                }
                                if (!isset($totalVAT)){
                                    $totalVAT = 0;
                                }
                                $discount = 1 - ($value['discount']/100);
                                $quantity = $value['amount'];
                                $price = $value['price'] * $quantity * $discount;
                                $totalprice = $totalprice + $price;
                                $VAT = $value['VAT']/100;
                                $VAT = $price * $VAT;
                                $totalVAT = $totalVAT + $VAT;
                            ?>
                            <td><?php echo $value['number'] ?></td>
                            <td><?php echo $value['cat'] ?></td>
                            <td><?php echo $value['name'] ?></td>
                            <td><?php echo $value['type'] ?></td>
                            <td><?php echo $value['price'] ?> </td>
                            <td><input type="text" value="<?php echo $quantity ?>" name="<?php echo 'amount'.array_search($value, $_SESSION['orderlist']); ?>" size="5"/></td>
                            <td><?php echo $value['VAT'] ?></td>
                            <td><input type="text" value="<?php echo $value['discount'] ?>" name="<?php echo 'discount'.array_search($value, $_SESSION['orderlist']); ?>" size="5"/></td>
                            <td style="text-align: right;"><?php echo $price ?></td>
                            <input type="hidden" name="<?php echo array_search($value, $_SESSION['orderlist']) ?>" value="<?php echo array_search($value, $_SESSION['orderlist']) ?>"/>
                        </tr>
                        
                        <input type="hidden" value="<?php echo $totalprice + $totalVAT; ?>" name="totalprice"/>
                        <?php } ?>
                        <tr>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td style="text-align: right;"><?php echo $totalprice ?></td>
                        </tr>
                        <tr>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td class="bold">VAT</td>
                            <td style="text-align: right;"><?php echo $totalVAT ?></td>
                        </tr>
                        <tr>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td class="bold">Total</td>
                            <td style="text-align: right;"><?php echo $totalprice + $totalVAT ?></td>
                        </tr>
                        <tr>
                            <td colspan="2"><input type="submit" name="update" value="" class="bt_update" title="Update order list"/></td>
                        </tr>
                        </form>
                        <input type="hidden" value="<?php echo $totalprice + $totalVAT; ?>" name="totalprice"/>
                    </table>
                    <table>
                        <tr>
                            <td><input type="submit" name="empty_order" value="" class="bt_empty" title="Empty entire order list"/></td>
                            <td><input type="submit" value="" name="page" class="ok ok_margin" title="Continue to submit order"/></td>
                        </tr>
                    </table>
                </form>
            </fieldset>
        <?php } ?>
        <?php
    } else {
        //show the fill in of contact 
        ?>
        <form action="<?php echo url_for('sales/' . ($form->getObject()->isNew() ? 'createOrder' : 'updateOrder') . (!$form->getObject()->isNew() ? '?id=' . $form->getObject()->getId() : '')) ?>" method="post" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>
            <?php if (!$form->getObject()->isNew()): ?>
                <input type="hidden" name="sf_method" value="put" />
            <?php endif; ?>
            <fieldset>
                <legend class="bold">General information</legend>
                <p style="color:red;">* required to be filled in</p>
                <table>
                    <?php $form->customizeStatus($status, $category);
                        $form->setCategory($category);  ?>
                    <tr>
                        <td class="left_title italic">Status: *</td>
                        <td class="left_input"><?php echo $form['status']->render() ?></td>
                        <td class="left_error"><?php echo $form['status']->renderError() ?></td>
                    </tr>
                    <tr>
                        <td class="left_title italic">Contact Order No.:</td>
                        <td class="left_input"><?php echo $form['c_order_number']->render() ?></td>
                        <td class="left_error"><?php echo $form['c_order_number']->renderError() ?></td>
                    </tr>
                    <tr>
                        <td class="left_title italic">Due date: *</td>
                        <td class="left_input"><?php echo $form['date_due']->render() ?></td>
                        <td class="left_error"><?php echo $form['date_due']->renderError() ?></td>
                    </tr>
                </table>
            </fieldset>
            <fieldset>
                <legend class="bold">Contact</legend>
                <table>
                    <tr>
                        <td class="left_title italic">Contact: *</td>
                        <td class="left_input"><?php echo $form['contact_id']->render() ?></td>
                        <td class="left_error"><?php echo $form['contact_id']->renderError() ?></td>
                    </tr>
                </table>
                <!-- search contact -->
            </fieldset>
            <fieldset>
                <legend class="bold">Shipping information</legend>
                <table>
                    <tr>
                        <td class="left_title italic">Last Name:</td>
                        <td class="left_input"><?php echo $form['ship_lastname']->render() ?></td>
                        <td class="left_error"><?php echo $form['ship_lastname']->renderError() ?></td>
                    </tr>
                    <tr>
                        <td class="left_title italic">First Name:</td>
                        <td class="left_input"><?php echo $form['ship_firstname']->render() ?></td>
                        <td class="left_error"><?php echo $form['ship_firstname']->renderError() ?></td>
                    </tr>
                    <tr>
                        <td class="left_title italic">Address:</td>
                        <td class="left_input"><?php echo $form['ship_address']->render() ?></td>
                        <td class="left_error"><?php echo $form['ship_address']->renderError() ?></td>
                    </tr>
                    <tr>
                        <td class="left_title italic">Zip code:</td>
                        <td class="left_input"><?php echo $form['ship_zipcode']->render() ?></td>
                        <td class="left_error"><?php echo $form['ship_zipcode']->renderError() ?></td>
                    </tr>
                    <tr>
                        <td class="left_title italic">City:</td>
                        <td class="left_input"><?php echo $form['ship_city']->render() ?></td>
                        <td class="left_error"><?php echo $form['ship_city']->renderError() ?></td>
                    </tr>
                    <tr>
                        <td class="left_title italic">Country:</td>
                        <td class="left_input"><?php echo $form['ship_country']->render() ?></td>
                        <td class="left_error"><?php echo $form['ship_country']->renderError() ?></td>
                    </tr>
                </table>
            </fieldset>
                
                <input type="submit" value="" class="ok" title="Confirm order"/>
                <span class="hidden_csrf">
                <?php $form->setTotalPrice($_SESSION['price']); ?>
                <?php echo $form['_csrf_token']->renderRow(); ?>
                <?php echo $form['date_recieved']->renderRow(); ?>
                <?php echo $form['total_price']->renderRow(); ?>
                <?php echo $form['date_ready']->renderRow(); ?>
                <?php echo $form['category_name']->renderRow(); ?>
                </span>
        </form>
        <?php
    }
?>
